package constraint;

import choco.Constraint;

import common.MCore;

import expression.MExpr;
import expression.MIntCst;
import expression.MIntExpr;

public class MLeq extends MCompare {

	public MLeq(MCore c, MIntExpr left, MIntExpr right) {
		super(c, left, right);
	}

	public Constraint constraint() {
		return getCore().getProblem().leq(getParameters()[0].eval().intExp(), getParameters()[1].eval().intExp());
	}

	public String toText() {
		return "(" + getParameters()[0].toText() + " <= " + getParameters()[1].toText() + ")";
	}

	public MConstraint eval() {
		MExpr le = getParameters()[0].eval();
		MExpr re = getParameters()[1].eval();
		
		if ((le instanceof MIntCst) && (re instanceof MIntCst)) {
			if (((MIntCst)le).compute() <= ((MIntCst)re).compute()) {
				return new MTrue(getCore());
			}
			else {
				return new MFalse(getCore());
			}
		}
		else {
			return new MLeq(getCore(), (MIntExpr)le, (MIntExpr)re);
		}
	}
}
